﻿Imports System.Data.OleDb
Imports System.IO
Imports System.ComponentModel


Public Class Frm_CashSend
    Private Kasa_Dosya As String
    Private Depo_No As String
    Public WithEvents bgworker As New BackgroundWorker
    Public HataVerdi As Boolean = False
    Public GonderilenKayitSayisi As Integer

    Sub New(ByVal kasa_dosyasi As String, ByVal depono As String)
        InitializeComponent()
        Kasa_Dosya = kasa_dosyasi
        Depo_No = depono
    End Sub

    Public Sub dowork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles bgworker.DoWork
        Dim prm() As String = CType(e.Argument, String())
        Stock_Exports()
    End Sub

    Private Sub bgworker_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgworker.ProgressChanged
        If e.ProgressPercentage > ProgressBar1.Maximum Then
            ProgressBar1.Value = ProgressBar1.Maximum
        Else
            ProgressBar1.Value = e.ProgressPercentage
        End If
        Label1.Text = "Aktarım devam ediyor..."
    End Sub

    Public Sub worker_complete(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles bgworker.RunWorkerCompleted
        If e.Error Is Nothing Then
            ProgressBar1.Value = ProgressBar1.Maximum
            Label1.Text = "Aktarım Tamamlandı."
        Else
            HataVerdi = True
        End If
    End Sub

    Private Sub Gonderildi()
        Dim cn As New OleDbConnection(Cnn_String)
        cn.Open()
        Dim cmd As New OleDbCommand
        cmd.Connection = cn
        cmd.CommandText = "UPDATE TBL_STOK SET TBL_STOK.kasaya_gonder = 0"
        cmd.ExecuteNonQuery()
        cn.Close()
    End Sub


    Private Sub Stock_Exports()
        Try
            Dim cn As New OleDbConnection(Cnn_String)
            cn.Open()
            Dim cmd As New OleDbCommand
            cmd.Connection = cn

            If RadioButton1.Checked = True Then 'tüm bilgiler
                cmd.CommandText = "SELECT * FROM Q_KasaAktarim"
                cmd.Parameters.Add("@depoid", OleDbType.VarChar)
                cmd.Parameters.Add("@tarih", OleDbType.Date)
                cmd.Parameters.Item("@depoid").Value = Depo_No
                cmd.Parameters.Item("@tarih").Value = New DateTime(1900, 1, 1, 0, 0, 0)
            ElseIf RadioButton2.Checked = True Then
                cmd.CommandText = "SELECT * FROM Q_KasaAktarim"
                cmd.Parameters.Add("@depoid", OleDbType.VarChar)
                cmd.Parameters.Add("@tarih", OleDbType.Date)
                cmd.Parameters.Item("@depoid").Value = Depo_No
                cmd.Parameters.Item("@tarih").Value = New DateTime(DateTimePicker1.Value.Year, DateTimePicker1.Value.Month, DateTimePicker1.Value.Day, 0, 0, 0)
            Else
                cmd.CommandText = "SELECT * FROM Q_KasaAktarim2"
                cmd.Parameters.Add("@depoid", OleDbType.VarChar)
                cmd.Parameters.Add("@kasa", OleDbType.Integer)
                cmd.Parameters.Item("@depoid").Value = Depo_No
                cmd.Parameters.Item("@kasa").Value = 1
            End If

            Dim adp As New OleDbDataAdapter(cmd)
            Dim dt As New DataTable
            adp.Fill(dt)
            cn.Close()

            Dim barkod As String = ""
            Dim stokad As String = ""
            Dim genelfiyat As Decimal = 0
            Dim depofiyat As Decimal = 0
            Dim genelfiyat2 As Decimal = 0
            Dim depofiyat2 As Decimal = 0
            Dim kullanilacakfiyat As Decimal = 0
            Dim kullanilacakfiyat2 As Decimal = 0
            Dim etikettipi As Integer = 0
            Dim barkodicerik As Integer = 0
            Dim birim1katsayi As Integer = 1
            Dim strbirim1katsayi As String = ""
            Dim birim1ad As String = "AD"
            Dim anagrupkod As String = ""
            Dim teraziflag As Integer = 0
            Dim decimalflag As Integer = 0
            Dim perakendekdv As Integer = 0
            Dim alternativeprice As String = ""
            Dim alternativeprice2 As String = ""

            Dim s_mntplu_dat_write As New StreamWriter(Kasa_Dosya, False, System.Text.Encoding.GetEncoding("ibm857"))
            Dim line As String = ""

            GonderilenKayitSayisi = dt.Rows.Count

            For i As Integer = 0 To dt.Rows.Count - 1
                barkod = dt.Rows(i)("barkod")
              
                stokad = dt.Rows(i)("stokad")
                If Not dt.Rows(i)("genelfiyat1") Is DBNull.Value Then genelfiyat = dt.Rows(i)("genelfiyat1") Else genelfiyat = 0
                If Not dt.Rows(i)("depofiyat1") Is DBNull.Value Then depofiyat = dt.Rows(i)("depofiyat1") Else depofiyat = 0
                If Not dt.Rows(i)("genelfiyat2") Is DBNull.Value Then genelfiyat2 = dt.Rows(i)("genelfiyat2") Else genelfiyat2 = 0
                If Not dt.Rows(i)("depofiyat2") Is DBNull.Value Then depofiyat2 = dt.Rows(i)("depofiyat2") Else depofiyat2 = 0
                'If Not dt.Rows(i)("barkodtipi") Is DBNull.Value Then etikettipi = dt.Rows(i)("barkodtipi") Else etikettipi = 0

                If depofiyat <= 0 Then
                    kullanilacakfiyat = genelfiyat
                Else
                    kullanilacakfiyat = depofiyat
                End If

                If depofiyat2 <= 0 Then
                    kullanilacakfiyat2 = genelfiyat2
                Else
                    kullanilacakfiyat2 = depofiyat2
                End If

                If Not dt.Rows(i)("barkodicerik") Is DBNull.Value Then barkodicerik = dt.Rows(i)("barkodicerik") Else barkodicerik = 0
                If Not dt.Rows(i)("birim1katsayi") Is DBNull.Value Then birim1katsayi = dt.Rows(i)("birim1katsayi") Else birim1katsayi = 1
                If Not dt.Rows(i)("birim1ad") Is DBNull.Value Then birim1ad = dt.Rows(i)("birim1ad") Else birim1ad = "AD"
                If Not dt.Rows(i)("anagrupkod") Is DBNull.Value Then anagrupkod = dt.Rows(i)("anagrupkod") Else anagrupkod = "0000"
                If Not dt.Rows(i)("perakendekdv") Is DBNull.Value Then perakendekdv = dt.Rows(i)("perakendekdv") Else perakendekdv = 0

                barkod = Mid(barkod, 1, 13)

                If anagrupkod.Length < 4 Then
                    anagrupkod = anagrupkod & Sifir(4 - anagrupkod.Length)
                Else
                    anagrupkod = Mid(anagrupkod, 1, 4)
                End If

                If barkodicerik = 1 Or barkodicerik = 2 Then
                    teraziflag = 2
                    decimalflag = 1
                    barkod = barkod & Sifir(12 - barkod.Length)
                    barkod = CreateChecksum(barkod)
                Else
                    If barkod.Length < 13 Then barkod = Space(13 - barkod.Length) & barkod
                    teraziflag = 0
                    decimalflag = 0
                End If

                If birim1ad.Length > 2 Then birim1ad = birim1ad.Substring(0, 2) Else birim1ad = "AD"
                If birim1katsayi <= 0 Then birim1katsayi = 1
                strbirim1katsayi = Format(birim1katsayi, "000")

                If stokad.Length < 20 Then stokad = stokad & Space(20 - stokad.Length) Else stokad = Mid(stokad, 1, 20)
                alternativeprice = Format(kullanilacakfiyat, "0##.00")
                alternativeprice = alternativeprice.Replace(",", "")
                alternativeprice = alternativeprice.Replace(".", "")
                alternativeprice = Sifir(9 - alternativeprice.Length) & alternativeprice

                alternativeprice2 = Format(kullanilacakfiyat2, "0##.00")
                alternativeprice2 = alternativeprice2.Replace(",", "")
                alternativeprice2 = alternativeprice2.Replace(".", "")
                alternativeprice2 = Sifir(12 - alternativeprice2.Length) & alternativeprice2

                line = "   " & barkod & anagrupkod & teraziflag & decimalflag & "0" & perakendekdv & "00" & birim1ad & strbirim1katsayi & "00000" & stokad & alternativeprice2 & " " & alternativeprice

                If line.Length <> 78 Then
                    Throw New ExecutionEngineException("hatali satır")
                Else
                    s_mntplu_dat_write.WriteLine(line)
                End If
            Next
            s_mntplu_dat_write.Close()


            Gonderildi()

        Catch ex As Exception
            MsgBox("Stoklar Gönderilemedi")
        End Try
    End Sub

    Private Function Sifir(ByVal miktar As Integer) As String
        Dim sonuc As String = ""
        For i As Integer = 1 To miktar
            sonuc &= "0"
        Next
        Return sonuc
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim str(2) As String
        str(0) = ""
        str(1) = ""
        str(2) = ""
        bgworker.WorkerReportsProgress = True
        bgworker.WorkerSupportsCancellation = True
        bgworker.RunWorkerAsync(str)
    End Sub
End Class